<?php
namespace app\serverfund\logic;

use \app\manage\logic\BaseLogic;

/**
 * @author:      李颖
 * @DateTime:    2017-06-28 17:46:11
 * @Description: 积分日志记录
 */
class IntegralOptionLog extends BaseLogic
{
    protected $name = 'integral_option_log';
    protected $config = 'db_config_fund';
    private $integralOptionLogModel = '\app\serverfund\model\IntegralOptionLog';

    // 所有用户积分操作日志分页数据
    public function list(array $data): array
    {
        $where['status'] = 1;
        $data['type'] && $where['type'] = $data['type'];
        $data['keywords'] && $where['remark|user_id'] = ['like', '%' . $data['keywords'] . '%'];
        
        if($data['startTime']){
            $where['create_time'] = $data['endTime'] ? ['between', [$data['startTime'],$data['endTime']]] : ['egt', $data['startTime']];
        }else{
            $data['endTime'] && $where['create_time'] = ['elt', $data['endTime']];
        } 
        $res = $this->model($this->integralOptionLogModel)
                    ->where($where)
                    ->order('id desc')
                    ->paginate($data['list_rows'], false, ['page'=>$data['page']])
                    ->toArray();
        return auto_return_page($res, '已找到用户积分操作日志', '没有用户积分操作日志');
    }
    // 获取某些user_ids的的积分操作日志(分页)
    public function listByUserIds(array $data): array
    {
        $where['status'] = 1;
        $where['user_id'] = ['IN', $data['user_ids']];
        $res = $this->model($this->integralOptionLogModel)
                    ->where($where)
                    ->order('id desc')
                    ->paginate($data['list_rows'], false, ['page'=>$data['page']])
                    ->toArray();
        return auto_return_page($res, '已找到用户积分操作日志', '没有用户积分操作日志');
    }
    // 获取某个用户某个状态的积分操作日志分页数据
    public function listByUserType(array $data): array
    {
        $where['status'] = 1;
        $where['user_id'] = $data['user_id'];
        if(isset($data['type']) && $data['type']){ $where['type'] = $data['type']; } 
        if(isset($data['integral'])){
            if($data['integral'] == 'income'){
                $where['integral'] = ['gt',0]; 
            }elseif($data['integral'] == 'expend'){
                $where['integral'] = ['lt',0]; 
            }
        }
        if(isset($data['time'])){
            $threeMonthTime = mktime(0, 0, 0, date("m")-3, date("d"), date("Y"));
            if($data['time'] == 'threeMonthAgo'){   
                $where['create_time'] = ['lt',$threeMonthTime];    //3个月以前
            }elseif($data['time'] == 'threeMonth'){   
                $where['create_time'] = ['gt',$threeMonthTime];    //3个月之内
            }
        }

        $res = $this->model($this->integralOptionLogModel)
                    ->where($where)
                    ->order('id desc')
                    ->paginate($data['list_rows'], false, ['page'=>$data['page']])
                    ->toArray();
        return auto_return_page($res, '已找到用户积分操作日志', '没有用户积分操作日志');
    }
}